// integral_shop/pages/shop_list/index.js
import { Model} from '../model.js'

const model=new Model();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    contentHeight:0,  //内容高度
    cid:0,
    category:[],
    list:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.cid){
      let cid=options.cid
      this.setData({
        cid
      })
    }
    wx.getSystemInfo({
      success: (res)=>{
        this.setData({
          contentHeight:res.windowHeight-40
        })
      }
    })
    
    this.getCategory()
  },

  getCategory(){
    let cid=this.data.cid;
    model.getCategory((res)=>{
      if(cid==0){
        cid=res.data[0].id
      }
      this.getCategoryShopList(cid);
      this.setData({
        cid,
        category:res.data
      });
    });
  },

  getCategoryShopList(cid){
    let list=this.data.list;
    let page=1;
    if(!list[cid]){
      model.getCategoryShopList(cid,page,(res)=>{
        list[cid]=res.data;
        this.setData({
          list
        });
      });
    }
  },

  onDetail(e){
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/index?id='+id,
    });
  },

  onCurrent(e){
    let cid=e.detail.activeKey
    this.setData({
      cid
    })
    this.getCategoryShopList(cid)
  },

  onLoadMore(e){
    let cid=this.data.cid
    let page=this.data.list[cid].page
    let list=this.data.list
    if(page){
      model.getCategoryShopList(cid,page,(res)=>{
        list[cid].data=list[cid].data.concat(res.data.data);
        list[cid].page=res.data.page
        this.setData({
          list
        });
      });
    }
  }
}) 